﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControllerLayer.Login;
using ModelLayer.Login;

using ViewLayer.MainView;

namespace ViewLayer.Login
{

    public partial class LoginView : Form
    {
        LoginController loginController = new LoginController();
        User user;
        int loginTime = 1;
       
        /// <summary>
        /// Esta vista permite loguear a los radiologos
        /// </summary>
        /// <param name="loginTimep">1 si es la primera vez q inicia sesion,2 si se va a re-loguear</param>
        /// <param name="name">si es la segunda vez que se loguea necesita cargar el username seleccionado</param>

        public LoginView(int loginTimep, string name)
        {
            user = User.Instance;
            loginTime = loginTimep;
         


            if (loginTime == 2)
            {
                InitializeComponent();
                this.textBoxUser.Text = name;
                this.textBoxUser.ReadOnly = true;
                this.textBoxPassword.Focus();
                this.ControlBox=false;
            }
            else
            {
                InitializeComponent();

            }
       



        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            //sql injection controller
            if ((loginController.LoginValidate(textBoxUser.Text) == false) && (loginController.LoginValidate(textBoxPassword.Text) == false))
            {
                ControlInicioSesion();
               
            }
            else
            {
                MessageBox.Show("Usuari@ y/o password invalidos",
                 "Error de conexion",
                 MessageBoxButtons.OK,
                 MessageBoxIcon.Exclamation);
                if (MainView.MainView.lastTimeLog == 1)
                {
                    textBoxPassword.Clear();
                    textBoxUser.Clear();
                    textBoxUser.Focus();
                }
                else
                {
                    this.textBoxPassword.Clear();
                    this.textBoxPassword.Focus();
                }
            }
   
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            
                ControllerLayer.Login.LoginController.exit = true;
                this.Close();
                   
            
        }

   
     

        private void CapsLock(object sender, KeyEventArgs e)
        {
            if (Control.IsKeyLocked(Keys.CapsLock))
            {

                this.label1CapsLock.Text = "Caps Lock activado.";
            }
            else {
                this.label1CapsLock.Text = "";
       
            }

        }


        private void ControlInicioSesion()
        {

            if (loginController.VerifyAccessType(1, this.textBoxUser.Text, textBoxPassword.Text, loginTime))
            {
                ControllerLayer.Login.LoginController.autentication = true;
                loginController.UpdateTimeEnter();

                this.Hide();


            }
            else
            {
                MessageBox.Show("Usuari@ y/o contraseña incorrectas",
                 "Error de conexion",
                 MessageBoxButtons.OK,
                 MessageBoxIcon.Exclamation);
                //user.Reset();
                if (MainView.MainView.lastTimeLog == 1)
                {
                    this.textBoxUser.Clear();
                    this.textBoxPassword.Clear();
                    textBoxUser.Focus();
                }
                else 
                {
                    this.textBoxPassword.Clear();
                    this.textBoxPassword.Focus();
                }


            }

        }

        private void textBoxUser_KeyDown(object sender, KeyEventArgs e)
        {
               if (e.KeyCode == Keys.Enter)
            {
               
                ControlInicioSesion();
            }
        }

        private void textBoxPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
               
                ControlInicioSesion();
            }
        }

        private void LoginView_FormClosed(object sender, FormClosedEventArgs e)
        {
            ControllerLayer.Login.LoginController.exit = true;
            this.Close(); 
       }

    
        


    }
}
